<?php

class DomObjDefinition implements IDomObjDefinition
{

  protected $definitions = array();

  /**
   *
   * @var string
   */
  protected $class;

  /**
   *
   * @var string
   */
  protected $name;

  /**
   *
   * @param string $class
   * @return void
   */
  public function __construct($name, array $definitions = array())
  {
    $this->name = $name;
    $this->definitions = $definitions;
  }

  public function addDefinition($type, $name, array $def)
  {
    if ($this->hasDefinition($name, $type)) {
      throw new DomainException("Duplicate domain object definition: <b>$name</b>");
    }

    $this->definitions[$type][$name] = $def;
  }

  public function hasDefinition($type, $name)
  {
    return isset($this->definitions[$type][$name]);
  }

  public function getDefinition($type, $name)
  {
    if (! $this->hasDefinition($type, $name)) {
      throw new DomainException("Domain object definition not found: <b>$name</b>");
    }

    return (array) $this->definitions[$type][$name];
  }

  public function factoryObject($type, $name)
  {
    throw new DomainException("Unknown object type in entity: <b>$type</b>");
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IDomObjDefinition#getName()
   */
  public function getName()
  {
    return $this->name;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IDomObjDefinition#getClass()
   */
  public function getClass()
  {
    if (empty($this->class)) {
      $this->class = $this->getName();
    }

    return $this->class;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IDomObjDefinition#factory()
   */
  public function factory()
  {
    $class = $this->getClass();
    $obj = new $class();
    return $obj;
  }

}

?>